﻿Public Class StorageManager

    Private SettingsDictionary As New Dictionary(Of Guid, Settings)
    Private LivePrograms As New List(Of LiveProgram)
    Private AlertGroups As New List(Of AlertGroup)

    Private Sub SetSettings(ByVal id As Guid, ByVal settings As Settings)
        Dim s = Serializer.Deserialize(Serializer.Serialize(settings))

        If SettingsDictionary.ContainsKey(id) Then
            SettingsDictionary(id) = s
        Else
            SettingsDictionary.Add(id, s)
        End If
    End Sub

    Public Function GetPluginSettings(ByVal id As Guid)
        If Not SettingsDictionary.ContainsKey(id) Then
            Return New Settings
        End If
        Return SettingsDictionary(id)
    End Function

    Public Sub SetPluginSettings(ByVal id As Guid, ByVal settings As Settings)
        SetSettings(id, settings)
    End Sub

    Public Function GetLivePrograms() As IList(Of LiveProgram)
        Return LivePrograms
    End Function

    Public Sub SetLivePrograms(ByVal programs As IList(Of LiveProgram))
        LivePrograms.Clear()
        For Each p In programs
            LivePrograms.Add(DirectCast(p.Clone, LiveProgram))
        Next
    End Sub

    Public Function GetAlertGroups() As IList(Of AlertGroup)
        Return AlertGroups
    End Function

    Public Sub SetAlertGroups(ByVal alertGroups As IList(Of AlertGroup))
        ' TODO copy
        Me.AlertGroups = alertGroups
    End Sub

    'Public Function GetAlertGroup(ByVal id As Guid) As AlertGroup

    'If Not SettingsDictionary.ContainsKey(id) Then
    'Return Nothing
    'End If

    'Dim s = SettingsDictionary(id)
    'Return s.GetValue(Of AlertGroup)("alertGroup")

    'End Function

    'Public Sub SetAlertGroup(ByVal alertGroup As AlertGroup)

    'Dim settings = New Settings
    'settings.Add("alertGroup", alertGroup)
    'SetSettings(alertGroup.Id, settings)

    'End Sub


    Public Function GetData(ByVal id As Guid) As Settings
        If Not SettingsDictionary.ContainsKey(id) Then
            Return Nothing
        End If
        Return SettingsDictionary(id)
    End Function

    Public Sub SetData(ByVal id As Guid, ByVal settings As Settings)
        SetSettings(id, settings)
    End Sub

    Public Sub Save()
        Dim settingsDoc = <?xml version="1.0" encoding="UTF-8"?>
                          <NamaAlert></NamaAlert>
        For Each keyValue In SettingsDictionary
            Dim el = Serializer.Serialize(keyValue.Value)
            el.@id = keyValue.Key.ToString
            settingsDoc.<NamaAlert>.FirstOrDefault.Add(el)
        Next

        Dim programsDoc = <?xml version="1.0" encoding="UTF-8"?>
                          <Programs></Programs>
        For Each p In LivePrograms
            Dim s = New Settings
            s.Add("LiveProgram", p)
            Dim el = Serializer.Serialize(s)
            el.@id = p.Id.ToString
            programsDoc.<Programs>.FirstOrDefault.Add(el)
        Next
    End Sub

    Public Sub Reload()
        SettingsDictionary.Clear()

    End Sub

End Class
